﻿using NetDh.Lib.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NetDh.Lib
{
    /// <summary>
    /// 维护在内存中的临时Log，最大长度是MACCOUNT。用于开发者记录测试。
    /// </summary>
    public class MemoryLog
    {
        private static ConcurrentQueue<MemoryLog> Logs = new ConcurrentQueue<MemoryLog>();
        private static long _incId = 0;
        private static int _count = 0;//ConcurrentQueue.Count是实时去计算的，会影响性能，因此此处增加_count的记录
        private static readonly int MACCOUNT = 10000;
        #region 实例
        public long Id { get; set; }
        public string Msg { get; set; }

        [JsonConverter(typeof(StringEnumConverter))]//在转Json字符串时，把枚举转为string，而不是int。
        public MemoryLogType LogType { get; set; }
        public int ThreadId { get; set; }
        public string CreateTime { get; set; }
        public MemoryLog()
        {
            Id = Interlocked.Increment(ref _incId);
            ThreadId = Thread.CurrentThread.ManagedThreadId;
            //CreateTime = DateTime.Now;
            CreateTime = DateTime.Now.ToStringX(true);
        }
        public MemoryLog(string msg, MemoryLogType logType = MemoryLogType.info) : this()
        {
            Msg = msg;
            LogType = logType;
        }
        #endregion
        public static int Count()
        {
            return _count;
        }
        public static void AddWarn(string msg)
        {
            Add(msg, MemoryLogType.warn);
        }
        public static void AddInfon(string msg)
        {
            Add(msg, MemoryLogType.info);
        }
        public static void Add(string msg, MemoryLogType logType = MemoryLogType.info)
        {
            var curCount = Interlocked.Increment(ref _count);
            Logs.Enqueue(new MemoryLog(msg, logType));
            if (curCount > MACCOUNT)
            {
                DelFirst();
            }
        }
        private static bool DelFirst()
        {
            Interlocked.Decrement(ref _count);
            MemoryLog del;
            return Logs.TryDequeue(out del);
        }
        public static IEnumerable<MemoryLog> GetLogs(string logType)
        {
            logType = logType != null ? logType.ToLower() : logType;
            var result = new List<MemoryLog>();
            if (string.IsNullOrEmpty(logType) || logType == "all")
            {
                return Logs.ToList();
                ////取全部类型
                //foreach (var log in Logs)
                //{
                //    result.Add(log);
                //}
            }
            else
            {
                foreach (var log in Logs)
                {
                    if (log.LogType.ToString() == logType)
                    {
                        result.Add(log);
                    }
                }
            }
            return result;
        }

        public static void DebugLog(string msg)
        {
            var info = "[contextId]:" + Thread.CurrentContext.ContextID + " [threadId]:" + Thread.CurrentThread.ManagedThreadId + " [msg]:" + msg;
            System.Diagnostics.Debug.WriteLine(info);
            //System.Diagnostics.Debug.Assert(1==0);//弹框并显示异常堆栈 。System.Diagnostics.Debug.Assert(true) 正常运行。只在Debgu模式下作用。
            //System.Diagnostics.Trace.WriteLine(info);
        }
    }

    public enum MemoryLogType
    {
        /// <summary>
        /// 信息记录。比如平台启动等
        /// </summary>
        info,
        /// <summary>
        /// 警告信息
        /// </summary>
        warn,
        /// <summary>
        /// 异常记录
        /// </summary>
        error,
        /// <summary>
        /// 用户操作日志。
        /// </summary>
        operation,
        /// <summary>
        /// 不合法请求
        /// </summary>
        illegal,
    }
}
